import _axios from 'axios'
import qs from  'qs';
import {mapkey,directionUrl} from '@s/webConfig.js'
import {MessageBox} from 'mint-ui'

//基本配置

const axios = _axios.create({
    headers:{
        'Content-Type':'application/x-www-form-urlencoded'
    },
    transformRequest:[function(data){
        data = qs.stringify(data);
            return data
    }],
    withCredentials:true
})
axios.defaults.withCredentials = true


let test = ()=>{
    axios.get('http://apis.map.qq.com/ws/place/v1/suggestion/?region=北京&keyword=美食&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77').then((data)=>{
        console.log(data)
    })
}



// let directionCb = (data)=>{
//     console.log(data)
// }
// const _direction = ({from,to})=>{
//     let paddingScript = document.getElementById('mapDirection');
//     let url = `http://apis.map.qq.com/ws/direction/v1/driving/?from=${from}&to=${to}&key=${mapkey}&output=jsonp&callback=directionCb`;

//     if(!paddingScript){
        
//         let headNode = document.querySelector('head');
//         paddingScript = document.createElement('script');
//         paddingScript.id = 'mapDirection';
//         headNode.appendChild(paddingScript)
//     }
//     paddingScript.src = url;
    
// }

//qqmap获取路线规划
const direction = ({
                from,
                to,
                waypoints=null,
                policy=null,
                key=mapkey,
                output='json'
            })=>{
    return new Promise((resolve,reject)=>{
        axios.get(directionUrl,{
            // baseURL: 'http://apis.map.qq.com/',
            params:{
                from,
                to,
                waypoints,
                policy,
                key,
                output
            },
            // withCredentials:true
        }).then((res)=>{
           
           console.log('http--direction:',res)
            resolve(res.data)
        })
    })
}

// 关键词输入提示
const searchNotice = ({keyword,region='中山'})=>{
    let params = {
        keyword,
        region,
        policy:1,
        filter:'category=美食,基础设施,房产小区,购物,汽车,酒店宾馆,银行金融',
        key:mapkey,
    };
    return new Promise((resolve,reject)=>{
        axios.get('/ws/place/v1/suggestion/',{params}).then((res)=>{
            console.log('http--searchNotice:',res)
            resolve(res.data)
        })
    })
}

/*账户接口
------------------------------------------------------------------------------------------------------- */
//获取验证码
const getCode = ({username,sms_type=1,send_type=1})=>{
    if(!username){
        console.log('username 不能为空！')
        return
    }

    let data = {
        username,
        sms_type,
        send_type
    };
    return new Promise((resolve,reject)=>{
        axios.post('/common/login/get_sms_code',data).then((res)=>{
           
           console.log('http--getCode:',res)
            resolve(res.data)
        })
    })
    
}

//注册
const register = ({username,password,phone_code,recommender=null,recommender_type=null})=>{
    
    let data = {
        username,
        password,
        phone_code,
        recommender,
        recommender_type

    };

    return new Promise((resolve,reject)=>{
        axios.post('/common/register',data).then((res)=>{
            console.log('http--register:',res)
            resolve(res.data)
        })
    })
}

//登录
const loginIn = ({username,password,device_id='FDA50693-A4E2-4FB1-AFCF-C6EB07647825'})=>{

        let data = {
            username,
            password,
            device_id
        };

        return new Promise((resolve,reject)=>{
            axios.post('/common/login',data).then((res)=>{
                console.log('http--loginIn:',res)
                if(res.data.code !=0){
                    MessageBox({
                        title: '提示',
                        message: res.data.msg,
                    })
                }
                resolve(res.data)
            })
        })
}

//退出登录
const loginOut = ({user_id})=>{

        let data = {
            user_id
        };

        return new Promise((resolve,reject)=>{
            axios.post('/common/login/loginout',data).then((res)=>{
                console.log('http--loginOut:',res)
                resolve(res.data)
            })
        })
}

const forgetPwd = ({username,password,sms_code})=>{
        let data = {
            username,
            password,
            sms_code
        };

        return new Promise((resolve,reject)=>{
            axios.post('/common/login/forget_pwd',data).then((res)=>{
                console.log('http--forgetPwd:',res)
                if(res.data.code !=0){
                    MessageBox({
                        title: '提示',
                        message: res.data.msg,
                    })
                }
                resolve(res.data)
            })
        })
    }
    
    /*乘客端快车接口
    ------------------------------------------------------------------------------------------------------- */
    // 车费预估
    const costEstimate = ({car_type=1,des_latitude,des_longitude,dep_latitude,dep_longitude,distance,duration,seat_num=null})=>{
        let data = {
            car_type,
            des_latitude,  //目的地
            des_longitude,
            dep_latitude,   //起点
            dep_longitude,
            distance,
            duration,
            seat_num
        };
        
        return new Promise((resolve,reject)=>{
            
            axios.post('/user/express/orders/cos_estimate',data).then((res)=>{
                console.log('http--costEstimate:',res)
                resolve(res.data)
            })
        })
}

//获取当前订单
const curOrder = ({user_id})=>{

        let data = {user_id}
        return new Promise((resolve,reject)=>{

            axios.post('/user/express/orders/current_order',data).then((res)=>{
                console.log('http--curOrder:',res)
                resolve(res.data)
            })
        })
}

//获取当前订单
const orderDetail = ({user_id,order_id})=>{

        let data = {user_id,order_id}
        return new Promise((resolve,reject)=>{

            axios.post('/user/express/orders/order_detail',data).then((res)=>{
                console.log('http--orderDetail:',res)
                resolve(res.data)
            })
        })
}


//乘客下单（实时单，叫快车）
const orederAdd = ({user_id,car_type='1',departure,dep_longitude,dep_latitude,destination,des_longitude,des_latitude,distance='0',duration='0',region='0760',driver_tips=null})=>{

        let data = {
            user_id,
            car_type,
            departure,
            dep_longitude,
            dep_latitude,
            destination,
            des_longitude,
            des_latitude,
            distance,
            duration,
            region,
            driver_tips
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/express/orders/add',data).then((res)=>{
                console.log('http--orederAdd:',res)
                resolve(res.data)
            })
        })
}
// 9.获取司机当前位置
const getCurDriverPos = (order_id)=>{
        let data = {
            order_id
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/express/orders/get_location',data).then((res)=>{
                console.log('http--getCurDriverPos:',res)
                resolve(res.data)
            })
        })
}
// 6.实时计费
const getOrderCost = (order_id)=>{
        let data = {
            order_id
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/express/orders/order_track',data).then((res)=>{
                console.log('http--getOrderCost:',res)
                resolve(res.data)
            })
        })
}


/*系统
------------------------------------------------------------------------------------------------------- */
// 21.个人资料
const getMyInfo = ()=>{
    return new Promise((resolve,reject)=>{
        
        axios.get('/user/user/account/my_information').then((res)=>{
            console.log('http--getMyInfo:',res)
            resolve(res.data)
        })
    })
}


// 3.支付方式
const getPayType = ({user_id,order_id})=>{
    let data = {
        user_id,order_id
    };
    return new Promise((resolve,reject)=>{
        
        axios.post('/user/pay/get_choose',data).then((res)=>{
            console.log('http--getPayType:',res)
            resolve(res.data)
        })
    })
}
// 4.支付确认  pay_type:1=支付宝 2=微信 6=余额
const paySubmit = ({order_id,pay_type=2,pay_coupon=null,wx_mp_id=null})=>{
    let data = {
        order_id,pay_type,wx_mp_id
    };
    if(pay_coupon){
        data.pay_coupon = pay_coupon
    }
    return new Promise((resolve,reject)=>{
        
        axios.post('/user/pay/submit',data).then((res)=>{
            console.log('http--paySubmit:',res)
            resolve(res.data)
        })
    })
}
//获取经纬度附近的司机
const getDriverNearby = ({longitude,latitude,order_type=1})=>{
        let data = {
            longitude,
            latitude,
            order_type
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/system/get_driver_near_position',data).then((res)=>{
                console.log('http--getDrivers:',res)
                resolve(res.data)
            })
        })
}



//添加常用地址
// type: 2家地址 3公司地址
const addCommonAddr = ({address,longitude,latitude,type,city=null,citycode=null})=>{
        let data = {
            address,
            longitude,
            latitude,
            type,
            city,
            citycode
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/user/address/add_common_address',data).then((res)=>{
                console.log('http--addCommonAddr:',res)
                resolve(res.data)
            })
        })
}
//获取常用地址
const getCommonAddr = ()=>{
    return new Promise((resolve,reject)=>{
        
        axios.post('/user/user/address/get_common_address').then((res)=>{
            console.log('http--getCommonAddr:',res)
            resolve(res.data)
        })
    })
}

//编辑常用地址
const editCommonAddr = ({user_id,address,address_id,longitude,latitude,type,city=null,citycode=null})=>{
        let data = {
            address,
            longitude,
            latitude,
            type,
            city,
            citycode
        };
        return new Promise((resolve,reject)=>{

            axios.post('/user/user/address/edit_common_address',data).then((res)=>{
                console.log('http--editCommonAddr:',res)
                resolve(res.data)
            })
        })
}

//删除常用地址
const delCommonAddr = (address_id)=>{
        let data = {address_id};
        return new Promise((resolve,reject)=>{

            axios.post('/user/user/address/del_common_address',data).then((res)=>{
                console.log('http--delCommonAddr:',res)
                
                resolve(res.data)
            })
        })
}



//我的行程（所有订单列表）
const myOrder = ({user_id,order_type=null,page=1,page_count=5})=>{
    let data = {
        user_id,order_type,page,page_count
    };
    return new Promise((resolve,reject)=>{

        axios.post('/user/user/myorder',data).then((res)=>{
            console.log('http--myOrder:',res)
            resolve(res.data)
        })
    })
}
// 14.评价司机
const assessment = ({user_id,order_id,star,content=null,labels,is_private=1})=>{
    let data = {
        user_id,order_id,star,content,labels,is_private
    };
    return new Promise((resolve,reject)=>{

        axios.post('/user/user/myorder/assessment',data).then((res)=>{
            console.log('http--assessment:',res)
            resolve(res.data)
        })
    })
}
// 15.乘客订单详情
const myOrderDetail = ({id,order_type})=>{
    let data = {
        id,order_type
    };
    return new Promise((resolve,reject)=>{

        axios.post('/user/user/myorder/order_detail',data).then((res)=>{
            console.log('http--myOrderDetail:',res)
            resolve(res.data)
        })
    })
}
// 16.删除订单
    const orderCancel = ({user_id,order_id})=>{
        let data = {
            user_id,order_id
        };
        return new Promise((resolve,reject)=>{
    
            axios.post('/user/user/myorder/cancel',data).then((res)=>{
                console.log('http--orderCancel:',res)

                resolve(res.data)
            })
        })
    }


//我的优惠券列表
const myCoupon = ({user_id,channel='',species='',page=1,page_count=5})=>{
    let data = {
        user_id,channel,species,page,page_count
    };
    return new Promise((resolve,reject)=>{

        axios.post('/user/user/wallet/coupons',data).then((res)=>{
            console.log('http--myCoupon:',res)
            resolve(res.data)
        })
    })
}

//33.费用详情
const feeDetail = (order_id)=>{
    let data = {
        order_id
    };
    return new Promise((resolve,reject)=>{

        axios.post('/user/user/myorder/fee_detail',data).then((res)=>{
            console.log('http--myCoupon:',res)
            resolve(res.data)
        })
    })
}

/*输出
------------------------------------------------------------------------------------------------------- */
export {
    test,
    getOpenId,
    direction,
    searchNotice,
    getCode,
    register,
    loginIn,
    loginOut,
    forgetPwd,

    costEstimate,
    curOrder,
    orderDetail,
    orederAdd,
    getCurDriverPos,
    getOrderCost,
    
    getMyInfo,
    getPayType,
    paySubmit,
    getDriverNearby,
    addCommonAddr,
    getCommonAddr,
    editCommonAddr,
    myOrder,
    myOrderDetail,
    orderCancel,
    assessment,
    myCoupon,
    feeDetail
}